//
//  RepositoriesViewController.m
//  GitBucket
//
//  Created by 3024 on 15/11/2.
//  Copyright © 2015年 ios25. All rights reserved.
//

#import "RepositoriesViewController.h"
#import "Repos1TableViewCell.h"
#import "OwnedModel1.h"
#import "JsonData.h"
#import "DetailViewController.h"
@interface RepositoriesViewController (){
    //数据源
    NSMutableArray *_dataSourse;
    OwnedModel1 *_model;
    JsonData *_jsondata;
}
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;

@property (strong, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation RepositoriesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _jsondata = [JsonData allowInitDefault];
    
    [self getDataSourse];
}
-(void)viewWillAppear:(BOOL)animated
{
    //显示底部
    self.tabBarController.tabBar.hidden = NO;
}

//获取数据源1
-(void)getDataSourse
{
    _dataSourse = [NSMutableArray new];

    /*一种方法
    NSMutableDictionary *reposDict = [NSMutableDictionary new];
    
    for (NSDictionary *dic in repos) {
        NSLog(@"%@",dic[@"full_name"]);
        [reposDict setObject:dic forKey:dic[@"f ull_name"]];
        NSLog(@"%@",reposDict);
        [_dataSourse addObject:reposDict[dic[@"full_name"]][@"full_name"]];
    }
     */
    NSMutableDictionary *dic = [NSMutableDictionary new];
    for (NSInteger i = 0; i < _jsondata.repos.count; ++i) {
        dic = _jsondata.repos[i];
        //有两个数字 好像不能直接取出来，先转换成字符串
        NSString * stargazers_count =[NSString stringWithFormat:@"%@",dic[@"stargazers_count"]];
        NSString * forks_count =[NSString stringWithFormat:@"%@",dic[@"forks_count"]];
        //传值用的字典
        NSDictionary *dataDic = dic;
        
        NSString *type = [NSString stringWithFormat:@"%@",dic[@"fork"]];
        UIImage *typeImage;
        if ([type isEqualToString:@"1"]) {
            typeImage = [UIImage imageNamed:@"bigFork.png"];
        }else
        {
            typeImage = [UIImage imageNamed:@"repo.png"];
        }

        _model = [OwnedModel1 allocInitMode:dic[@"name"] detail:dic[@"description"] starNumber:stargazers_count xNumber:forks_count typeImage:typeImage dataDic:dataDic];
        [_dataSourse addObject:_model];
    
    }
    NSLog(@"%@",_dataSourse);
    [self.tableView reloadData];
    
//    NSLog(@"%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"starred.json"]);
    
}
//获取数据源2
-(void)getDataSourse2
{
    _dataSourse = [NSMutableArray new];

    NSMutableDictionary *dic = [NSMutableDictionary new];
    for (NSInteger i = 0; i < _jsondata.starreds.count; ++i) {
        dic = _jsondata.starreds[i];
        //有两个数字 好像不能直接取出来，先转换成字符串
        NSString * stargazers_count =[NSString stringWithFormat:@"%@",dic[@"stargazers_count"]];
        NSString * forks_count =[NSString stringWithFormat:@"%@",dic[@"forks_count"]];
        //传值用的字典
        NSDictionary *dataDic = dic;
        NSString *type = [NSString stringWithFormat:@"%@",dic[@"fork"]];
        UIImage *typeImage;
        if ([type isEqualToString:@"1"]) {
            typeImage = [UIImage imageNamed:@"bigFork.png"];
        }else
        {
            typeImage = [UIImage imageNamed:@"repo.png"];
        }
        _model = [OwnedModel1 allocInitMode:dic[@"full_name"] detail:dic[@"description"] starNumber:stargazers_count xNumber:forks_count typeImage:typeImage dataDic:dataDic];
        [_dataSourse addObject:_model];
    }
    NSLog(@"%@",_dataSourse);
    [self.tableView reloadData];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
   
}

//segmentedControChange方法


- (IBAction)segmentedControlChange:(id)sender {
    UISegmentedControl *se = sender;
    switch (se.selectedSegmentIndex) {
        case 0:
            //Owned的时候
            NSLog(@"Owned");
            [self getDataSourse];
            break;
        case 1:
            //Starred的时候
            NSLog(@"Starred");
            [self getDataSourse2];
            break;
        default:
            break;
    }
    
}
//表视图外观代理
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 66;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//表视图数据源代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourse.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Repos1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"repositoriesCell1"];
    
    cell.model = _dataSourse[indexPath.row];
   
    return cell;
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    DetailViewController *dvc = segue.destinationViewController;
    Repos1TableViewCell *cell = sender;
    dvc.dataDic = cell.dataDic;
    
}


@end
